package com.zzyl.controller.customer;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.controller.BaseController;
import com.zzyl.dto.ReservationDto;
import com.zzyl.entity.CountByTime;
import com.zzyl.entity.Reservation;
import com.zzyl.service.CustomerReservationService;
import com.zzyl.vo.CountByTimeVo;
import com.zzyl.vo.ReservationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;

@RestController
@RequestMapping("/customer/reservation")
public class CustomerReservationController extends BaseController {
    @Autowired
    private CustomerReservationService customerReservationService;

    @GetMapping("/cancelled-count")
    public ResponseResult cancellerCount(){
        Integer count = customerReservationService.findCancellerCount();
        return success(count);
    }

    @GetMapping("/page")
    public ResponseResult<ReservationVo> findByPage(
            @RequestParam(name = "status",required = false)Integer status,
            @RequestParam(name = "pageNum",defaultValue = "1")Integer pageNum,
            @RequestParam(name = "pageSize",defaultValue = "10")Integer pageSize
    ){
        PageResponse<ReservationVo> vo = customerReservationService.findByPage(status,pageNum,pageSize);
        return success(vo);
    }

    @PostMapping
    public ResponseResult save(@RequestBody ReservationDto dto){
        Reservation reservation = BeanUtil.toBean(dto, Reservation.class);
        customerReservationService.save(reservation);
        return success();
    }

    @PutMapping("/{id}/cancel")
    public ResponseResult updateStatus(@PathVariable(name = "id")Long id){
        customerReservationService.updateStatus(id);
        return success();
    }

    @GetMapping("/countByTime")
    public ResponseResult<CountByTimeVo> selectCountByTime(@RequestParam(name = "time")Long time){
        Instant instant = Instant.ofEpochMilli(time);
        LocalDateTime begin = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).plusHours(8);
        LocalDateTime end = begin.plusHours(18);
        List<CountByTime> list = customerReservationService.selectCountByTime(begin,end);
        return success(list);
    }

}
